//
//  MeasurtimeparamVc.m
//  CareRing
//
//  Created  on 2024/12/17.
//
    

#import "MeasurtimeparamVc.h"
#import <Masonry/Masonry.h>
#import "ConfigModel.h"
#import "Colors.h"
#import "CARingSDK.h"
#import "CABLeService.h"
#import "FunctionButton.h"
#import "CARingSDK+Desc.h"
#import "CADeviceInfo+description.h"
#import "UIButton+demoStyle.h"
#import "CABLeService.h"

@interface MeasurtimeparamVc ()<CABleDataProtocal, CABleScanProtocal>
@property (weak, nonatomic) IBOutlet UISegmentedControl *segment;
@property (weak, nonatomic) IBOutlet UILabel *noSleepDuraLbl;
@property (weak, nonatomic) IBOutlet UITextField *noSleepDuraTf;
@property (weak, nonatomic) IBOutlet UILabel *noSleepIntervalLbl;
@property (weak, nonatomic) IBOutlet UITextField *noSleepIntervalTf;
@property (weak, nonatomic) IBOutlet UILabel *sleepMeaDuraLbl;
@property (weak, nonatomic) IBOutlet UITextField *sleepMeaDuraTf;
@property (weak, nonatomic) IBOutlet UILabel *sleepMeaIntervalLbl;
@property (weak, nonatomic) IBOutlet UITextField *sleepMeaIntervalTf;
@property (weak, nonatomic) IBOutlet UIButton *readBtn;
@property (weak, nonatomic) IBOutlet UIButton *writeBtn;



@end

@implementation MeasurtimeparamVc

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
    
}

-(void)initUI
{
    WEAK_SELF
    [self customNavStyleNormal:_L2(L_MEA_TIME_SET) BackBlk:^{
        STRONG_SELF
        [strongSelf.navigationController popViewControllerAnimated:YES];
    }];
    self.segment.selectedSegmentIndex = 0;
    
    [self.readBtn functionStyle];
    [self.writeBtn functionStyle];
    
    [self.readBtn setTitle:@"Read" forState:UIControlStateNormal];
    [self.writeBtn setTitle:@"Write" forState:UIControlStateNormal];

    self.noSleepDuraLbl.text = _L2(L_NO_SLEEP_DURA);
    self.noSleepIntervalLbl.text = _L2(L_NO_SLEEP_INTVAL);
    self.sleepMeaDuraLbl.text = _L2(L_SLEEP_MEA_DURA);
    self.sleepMeaIntervalLbl.text = _L2(L_SLEEP_MEA_INTVAL);
    
    self.noSleepDuraLbl.numberOfLines = 0;
    self.noSleepDuraLbl.adjustsFontSizeToFitWidth = YES;
    
    self.noSleepIntervalLbl.numberOfLines = 0;
    self.noSleepIntervalLbl.adjustsFontSizeToFitWidth = YES;
    
    self.sleepMeaDuraLbl.numberOfLines = 0;
    self.sleepMeaDuraLbl.adjustsFontSizeToFitWidth = YES;
    
    self.sleepMeaIntervalLbl.numberOfLines = 0;
    self.sleepMeaIntervalLbl.adjustsFontSizeToFitWidth = YES;

}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    CARingSDK.instance.blescanDelegate = self;
    CARingSDK.instance.bleDataDelegate = self;

}

- (IBAction)readClick:(id)sender {
    
    if (!CARingSDK.instance.currentDevice) {
        [self showAlertWarningWithTitle:@"Tips" Msg:_L2(L_TIP_BLE_NOT_CONNECT) btnCancel:_L2(L_OK) Compelete:nil];
        return;
    }
    
    CACS_DEV_MEA_DURA_AND_TM_TYPE type = CACS_DEV_MEA_DURA_AND_TM_TYPE_A;
    
    if (self.segment.selectedSegmentIndex == 1) {
        type = CACS_DEV_MEA_DURA_AND_TM_TYPE_B;

    }
    
    [CARingSDK.instance functionGetNalgDevMeaDuration:type];
    
}

- (IBAction)writeCLick:(id)sender {
    
    if (!CARingSDK.instance.currentDevice) {
        [self showAlertWarningWithTitle:@"Tips" Msg:_L2(L_TIP_BLE_NOT_CONNECT) btnCancel:_L2(L_OK) Compelete:nil];
        return;
    }
    
    
    CACS_DEV_MEA_DURA_AND_TM_TYPE type = CACS_DEV_MEA_DURA_AND_TM_TYPE_A;
    
    if (self.segment.selectedSegmentIndex == 1) {
        type = CACS_DEV_MEA_DURA_AND_TM_TYPE_B;

    }
    
    uint32_t meaduraNoSleep = (uint32_t)[self.noSleepDuraTf.text integerValue] ;
    uint32_t meaIntervNoSleep = (uint32_t)[self.noSleepIntervalTf.text integerValue];

    uint32_t meaduraInSleep = (uint32_t)[self.sleepMeaDuraTf.text integerValue];
    uint32_t meaIntervInSleep = (uint32_t)[self.sleepMeaIntervalTf.text integerValue];
    
    [CARingSDK.instance functionSetNalgDevMeaDuration:type MeaDuraofNoSlepPerd:meaduraNoSleep MeaIntervOfNoSlepPerd:meaIntervNoSleep MeaDuraofInSlepPerd:meaduraInSleep MeaIntervInNoSlepPerd:meaIntervInSleep];
    
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];

}

- (void)caBleCmdExcute:(EXCUTED_CMD)cmd Succ:(BOOL)isSucc Reason:(CA_CMD_EXECTE_ERROR_REASON)reason Data:(nonnull NSData *)originData
{
    
}

- (void)caBleDeviceBatteryLevel:(NSInteger)batteryLevel IsCharging:(BOOL)isCharging { 
    
}

- (void)caBleDeviceDidReadyForReadAndWrite:(nonnull CABLeService *)service { 
    
}

- (void)caBleDeviceInfo:(nonnull CADeviceInfo *)devInfo { 
    
}



- (void)caBleDeviceRealtimeTemperature:(nonnull NSNumber *)temperature { 
    
}

- (void)caBleDidConnectPeripheral:(nonnull CABLeService *)service { 
    
}

- (void)caBleDidDisconnectPeripheral:(nonnull CABLeService *)service { 
    
}

- (void)caBleFailConnectPeripheral:(nonnull CBPeripheral *)peripheral Error:(nonnull NSError *)error { 
    
}

- (void)caBleHistoryDataCount:(NSInteger)count { 
    
}

- (void)srBleHistoryDataTimeout { 
    
}

- (void)caBleHistorySr03Data:(nonnull CAHistoryDataSR03 *)hisData IsComplete:(BOOL)isComplete { 
    
}

- (void)caBleIsBinded:(BOOL)isBinded { 
    
}

- (void)caBleMeasureDuration:(NSInteger)seconds { 
    
}

- (void)caBleOEMAuthResult:(BOOL)authSucceddful { 
    
}

- (void)caBleRealtimeHRV:(nonnull NSNumber *)hrv BloodOxygen:(nonnull NSNumber *)bloodOxygen HeartRate:(nonnull NSNumber *)hr Status:(REALTIME_STATUS)status { 
    
}

- (void)caBleSN:(nonnull NSString *)sn SnData:(nonnull NSData *)snData { 
    
}

-(void)caBleNALGDevMeaDuration:(CACS_DEV_MEA_DURA_AND_TM_TYPE)meaType MeaDuraofNoSlepPerd:(uint32_t)meaduraNoSleep MeaIntervOfNoSlepPerd:(uint32_t)meaIntervNoSleep MeaDuraofInSlepPerd:(uint32_t)meaduraInSleep MeaIntervInNoSlepPerd:(uint32_t)meaIntervInSleep
{
    if (meaType == CACS_DEV_MEA_DURA_AND_TM_TYPE_A) {
        self.segment.selectedSegmentIndex = 0;
    }
    
    if (meaType == CACS_DEV_MEA_DURA_AND_TM_TYPE_B) {
        self.segment.selectedSegmentIndex = 1;
    }
    
    self.noSleepDuraTf.text = [NSString stringWithFormat:@"%u", meaduraNoSleep];
    self.noSleepIntervalTf.text = [NSString stringWithFormat:@"%u", meaIntervNoSleep];

    self.sleepMeaDuraTf.text = [NSString stringWithFormat:@"%u", meaduraInSleep];
    self.sleepMeaIntervalTf.text = [NSString stringWithFormat:@"%u", meaIntervInSleep];

    
}

@end
